<template>
  <Dialog
    title="产品规格列表"
    v-model="dialogVisible"
    ref="ConfigSpecRef"
    :append-to-body="false"
    :lock-scroll="false"
    width="1200"
    class="enhanced-dialog"
  >
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:setting" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">产品规格列表</h4>
        <p class="dialog-subtitle">管理产品的规格配置信息</p>
      </div>
    </div>

    <!-- 操作区域 -->
    <div class="action-container">
      <div class="action-header">
        <div class="header-info">
          <Icon icon="ep:list" class="info-icon" />
          <span class="info-text">规格配置</span>
        </div>
        <el-button
          type="primary"
          plain
          @click="openSkuForm('create', productId)"
          v-hasPermi="['system:product:create']"
          class="add-btn"
        >
          <Icon icon="ep:plus" class="btn-icon" />
          新增规格
        </el-button>
      </div>
    </div>

    <!-- 列表区域 -->
    <div class="table-container">
      <ContentWrap class="table-wrapper">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column label="规格编号" align="center" prop="id">
            <template #default="scope">
              <div class="id-cell">
                <Icon icon="ep:key" class="cell-icon" />
                <span class="id-text">{{ scope.row.id }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="产品名称" align="center" prop="name">
            <template #default="scope">
              <div class="name-cell">
                <Icon icon="ep:goods" class="cell-icon" />
                <span class="cell-text">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="规格类型" align="center" prop="type">
            <template #default="scope">
              <div class="type-cell">
                <el-tag v-if="scope.row.type == 1" class="enhanced-tag quantity-tag">
                  <Icon icon="ep:collection" class="tag-icon" />
                  指定数量
                </el-tag>
                <el-tag v-if="scope.row.type == 2" type="success" class="enhanced-tag custom-tag">
                  <Icon icon="ep:edit" class="tag-icon" />
                  自定义数量
                </el-tag>
                <el-tag v-if="scope.row.type == 3" type="warning" class="enhanced-tag renew-tag">
                  <Icon icon="ep:refresh" class="tag-icon" />
                  单个续费
                </el-tag>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="规格数量" align="center" prop="num">
            <template #default="scope">
              <div class="num-cell">
                <Icon icon="ep:collection" class="cell-icon" />
                <span v-if="scope.row.num" class="num-text">{{ scope.row.num }}</span>
                <span v-else class="empty-text">-</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="原价" align="center" prop="oldPrice">
            <template #default="scope">
              <div class="price-cell">
                <Icon icon="ep:money" class="cell-icon" />
                <span v-if="scope.row.oldPrice" class="old-price-text"
                  >¥{{ scope.row.oldPrice }}</span
                >
                <span v-else class="empty-text">-</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="现价" align="center" prop="price">
            <template #default="scope">
              <div class="price-cell">
                <Icon icon="ep:money" class="cell-icon" />
                <span class="current-price-text">¥{{ scope.row.price }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="有效期(天)" align="center" prop="expireTime">
            <template #default="scope">
              <div class="expire-cell">
                <Icon icon="ep:timer" class="cell-icon" />
                <span
                  class="expire-text"
                  :class="scope.row.expireTime == 0 ? 'permanent' : 'limited'"
                >
                  {{ scope.row.expireTime == 0 ? '永久' : scope.row.expireTime }}
                </span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template #default="scope">
              <div class="action-cell">
                <el-button
                  link
                  type="primary"
                  @click="openSkuForm('update', productId, scope.row.id)"
                  v-hasPermi="['system:product:update']"
                  class="action-button edit-btn"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>
                <el-button
                  link
                  type="danger"
                  @click="delSku(scope.row.id)"
                  v-hasPermi="['system:product:update']"
                  class="action-button delete-btn"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </ContentWrap>
    </div>
  </Dialog>

  <ConfigSpecForm ref="skuFormRef" @success="open" />
</template>

<script setup lang="ts">
// import { getProduct } from '@/api/system/product/index'
import ConfigSpecForm from './ConfigSpecForm.vue'
import * as ProductSkuApi from '@/api/system/productsku'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const ConfigSpecRef = ref() // 表单 Ref
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据]
const productId = ref(null)
/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  productId.value = id
  getList(id)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async (id) => {
  loading.value = true
  try {
    const data = await ProductSkuApi.getProductSkuList(id)
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const skuFormRef = ref()
const openSkuForm = (type: string, productId?: number, skuId?: number) => {
  skuFormRef.value.open(type, productId, skuId)
}

/** 删除按钮操作 */
const delSku = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductSkuApi.deleteProductSku(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList(productId.value)
  } catch {}
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
      max-height: 90vh;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
    max-height: 80vh;
    overflow-y: auto;

    @media (max-width: 768px) {
      max-height: 75vh;
    }
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 操作区域 */
.action-container {
  padding: 20px 24px;
  background: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }
}

.action-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.header-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-icon {
  font-size: 16px;
  color: $primary-color;
}

.info-text {
  font-size: 16px;
  font-weight: 600;
  color: $text-primary;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.add-btn {
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 10px 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    background: $primary-color;
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 表格容器 */
.table-container {
  background: white;
  overflow-x: auto;
}

.table-wrapper {
  :deep(.el-card__body) {
    padding: 0;
  }
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.id-cell,
.name-cell,
.num-cell,
.price-cell,
.expire-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.type-cell {
  display: flex;
  justify-content: center;
}

.action-cell {
  display: flex;
  gap: 8px;
  justify-content: center;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 4px;
  }
}

.cell-icon {
  font-size: 14px;
  flex-shrink: 0;
  color: $primary-color;
}

.cell-text {
  color: $text-regular;
  font-weight: 500;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.id-text {
  color: $primary-color;
  font-weight: 600;
  font-family: 'Courier New', monospace;
  background: $primary-light;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

.num-text {
  color: $success-color;
  font-weight: 600;
  background: rgba(103, 194, 58, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

.old-price-text {
  color: $text-secondary;
  font-weight: 500;
  text-decoration: line-through;
  background: rgba(144, 147, 153, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

.current-price-text {
  color: $danger-color;
  font-weight: 600;
  font-family: 'Courier New', monospace;
  background: rgba(245, 108, 108, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

.expire-text {
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;

  &.permanent {
    color: $success-color;
    background: rgba(103, 194, 58, 0.1);
  }

  &.limited {
    color: $warning-color;
    background: rgba(230, 162, 60, 0.1);
  }

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 1px 4px;
  }
}

.empty-text {
  color: $text-placeholder;
  font-style: italic;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

/* 标签样式 */
.enhanced-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
  font-size: 12px;
  border: none;
  box-shadow: $shadow-base;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }

  .tag-icon {
    font-size: 12px;

    @media (max-width: 768px) {
      font-size: 11px;
    }
  }

  &.quantity-tag {
    background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
    color: white;
  }

  &.custom-tag {
    background: linear-gradient(135deg, $success-color 0%, #52c41a 100%);
    color: white;
  }

  &.renew-tag {
    background: linear-gradient(135deg, $warning-color 0%, #faad14 100%);
    color: white;
  }
}

/* 操作按钮 */
.action-button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
    width: 100%;
    justify-content: center;
  }

  &.edit-btn:hover {
    background: rgba(0, 82, 204, 0.1);
    color: $primary-color;
  }

  &.delete-btn:hover {
    background: rgba(245, 108, 108, 0.1);
    color: $danger-color;
  }
}

/* 滚动条美化 */
:deep(.el-dialog__body) {
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .action-container,
  .table-container {
    background: #374151;
    border-color: #4b5563;
  }

  .info-text {
    color: #f9fafb;
  }

  .cell-text {
    color: #f9fafb;
  }

  .id-text {
    background: rgba(96, 165, 250, 0.1);
    color: #60a5fa;
  }

  .num-text {
    background: rgba(52, 211, 153, 0.1);
    color: #34d399;
  }

  .old-price-text {
    background: rgba(156, 163, 175, 0.1);
    color: #9ca3af;
  }

  .current-price-text {
    background: rgba(248, 113, 113, 0.1);
    color: #f87171;
  }

  .expire-text {
    &.permanent {
      background: rgba(52, 211, 153, 0.1);
      color: #34d399;
    }

    &.limited {
      background: rgba(251, 191, 36, 0.1);
      color: #fbbf24;
    }
  }

  .empty-text {
    color: #6b7280;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-tag {
    &.quantity-tag {
      background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
    }

    &.custom-tag {
      background: linear-gradient(135deg, #34d399 0%, #10b981 100%);
    }

    &.renew-tag {
      background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .action-container {
    padding: 12px 16px;
  }

  .info-text {
    font-size: 13px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .cell-text {
    font-size: 11px;
  }

  .id-text,
  .num-text,
  .old-price-text,
  .current-price-text,
  .expire-text {
    font-size: 10px;
  }

  .action-button {
    padding: 3px 6px;
    font-size: 10px;
  }

  .enhanced-tag {
    font-size: 10px;
    padding: 2px 4px;

    .tag-icon {
      font-size: 10px;
    }
  }
}
</style>
